package com.sk.bilibili.view.fragment;

import static com.sk.bilibili.utils.StringUtils.str;

import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.videoSet.discuss.DiscussAdapter;
import com.sk.bilibili.bean.Discuss;
import com.sk.bilibili.callBack.OnBottomCallBack;
import com.sk.bilibili.callBack.OnScrollBottomListener;
import com.sk.bilibili.databinding.FragmentDiscussBinding;
import com.sk.bilibili.model.fragment.DiscussModel;
import com.sk.bilibili.utils.PopupMenuUtils;
import com.sk.bilibili.view.activity.MemberActivity;
import com.sk.bilibili.view.diy.bottomDialog.DeleteBottomDialog;

import java.util.List;

public class DiscussFragment extends BaseFragment<FragmentDiscussBinding,DiscussModel> implements SwipeRefreshLayout.OnRefreshListener, OnBottomCallBack {

    private Integer targetId;
    // 0  视频集    1 番剧
    private Integer type;

    public DiscussFragment(Integer targetId, Integer type) {
        this.targetId = targetId;
        this.type = type;
    }

    private DiscussAdapter discussAdapter;

    @Override
    protected void afterSetView() {
        initView();
        bindEvent();
        loadData();
    }

    private void loadData() {
        model.loadDiscuss(targetId,type);
    }

    private void bindEvent() {
        bind.srlShow.setOnRefreshListener(this);
        bind.rvShow.addOnScrollListener(new OnScrollBottomListener(this));
        bind.tvSend.setOnClickListener(v -> model.sendDiscuss(str(bind.etSend),targetId,type));
    }

    private void initView() {
        bind.srlShow.setColorSchemeResources(R.color.pink);
        VirtualLayoutManager manager = new VirtualLayoutManager(getContext());
        bind.rvShow.setLayoutManager(manager);
        DelegateAdapter adapter = new DelegateAdapter(manager);
        discussAdapter=new DiscussAdapter(getContext(),this);
        adapter.addAdapter(discussAdapter);
        bind.rvShow.setAdapter(adapter);
    }

    public void addDiscuss(List<Discuss> records) {
        if(records.isEmpty()){
            toast("没有更多数据了");
        }else {
            discussAdapter.addData(records);
        }
    }

    public void setDiscuss(List<Discuss> records) {
        discussAdapter.setData(records);
        bind.srlShow.setRefreshing(false);
    }

    public void onClickMember(Integer id) {
        MemberActivity.start(getContext(),id);
    }

    public void onPraiseClick(Integer id) {
        model.praise(id);
    }

    public void onFocusClick(Integer id) {
        model.switchFocus(id);
    }

    public void sendSuccess() {
        toast("发送成功");
        onRefresh();
        bind.etSend.setText("");
    }

    @Override
    public void onRefresh() {
        bind.srlShow.setRefreshing(true);
        model.refreshDiscuss(targetId,type);
    }

    @Override
    public void onBottom() {
        model.loadDiscuss(targetId, type);
    }

    public void switchFocusSuccess(boolean value) {
        toast(value?"关注成功":"已经取消关注");
    }

    public void onMoreClick(View v, Integer id) {
        PopupMenuUtils.showPopupMenu(getContext(), v, R.menu.delete_menu, item -> {
            model.deleteDiscuss(id);
            return true;
        });
    }

    public void deleteDiscussSuccess() {
        onRefresh();
        toast("评论删除成功");
    }

}